define('views/publish', ['text!/views/publish.html', 'components/left'], function(template, Left) {
  var MaxWidth = 160;
  var MaxHeight = 160;
  return {
    template: template,
    replace: true,
    data: function() {
      return {
        init: true,
        files: [],
        uploadBlockFocus: false,
        leftData: {},
        data: {
          reduction: 1,
          iName: '',
          des: '',
          place: '',
          cid: 1,
          telphone: '',
          qq: '',
          price: 0
        }
      }
    },
    methods: {
      submit: submit,
      drop: function(e) {
        var vm = this;

        vm.uploadBlockFocus = false;
        var files = e.target.files || e.dataTransfer.files;
        for (var i = files.length; i > 0; i--) {
          vm.pushFile(files[i - 1]);
        }
      },
      pushFile: function(file) {
        var vm = this;
        var reader = new FileReader();
        reader.onload = function(e) {
          file.data = e.target.result;
          if (vm.files.length < 5) {
            if (file.size > 1048576 * 2) {
              alert('图片要小于2M');
              return;
            }
            vm.$set('files', vm.files.concat(file));
          }
        };
        reader.readAsDataURL(file);
      }
    },
    props: ['small', 'user', 'box'],
    created: function() {
      var vm = this;
      vm.leftData = vm.$parent.$data;
    },
    route: {
      data: function(transition){
        var vm = this;
        setTimeout(function(){
          if (vm.user.qq === '') {
            if(confirm('先去个人中心填好QQ号码吧,再来发布吧。')){
              vm.$router.go({
                name: 'profile'
              });
            }
          }
        }, 100);
        transition.next();
      }
    },
    computed: {
      pic0: function() {
        return this.files[0].data;
      },
      pic1: function() {
        return this.files[1].data;
      },
      pic2: function() {
        return this.files[2].data;
      },
      pic3: function() {
        return this.files[3].data;
      },
      pic4: function() {
        return this.files[4].data;
      }
    },
    components: {
      'app-left': Left
    }
  };

  function submit() {
    var vm = this;
    var data = vm.data;
    if (data.iName === '') {
      alert('商品名称不可为空！');
      return;
    }
    if (data.iName.length > 25) {
      alert('商品名称太长！');
      return;
    }
    if (data.des.length < 10) {
      alert('商品描述至少10个字！');
      return;
    }
    if (data.des.length > 200) {
      alert('商品描述不可超过200个字');
      return;
    }
    if (data.place === '') {
      alert('交易地点不可为空');
      return;
    }
    if (data.place.length > 50) {
      alert('交易地点不可超过50个字');
      return;
    }
    if (data.price === '') {
      alert('价格不能空着！');
      return;
    }
    if (data.price > 2000000) {
      alert('价格太离谱就在描述里写详情。');
      return;
    }
    if (vm.user.qq === '') {
      alert('先去个人中心填好QQ号码吧');
      return;
    }
    if (vm.pic0 === undefined) {
      alert('别忘了上传商品展示图');
      return;
    }
    vm.$http.post('/item/add', {
      reduction: data.reduction,
      iName: data.iName,
      des: data.des,
      place: data.place,
      cid: data.cid,
      pic0: vm.pic0,
      pic1: vm.pic1,
      pic2: vm.pic2,
      pic3: vm.pic3,
      pic4: vm.pic4,
      price: data.price
    }).then(publishOK);
  }

  function publishOK(res) {
    var res = res.data;
    if(res.code !== 0){
      alert(res.message);
      return;
    }
    this.$router.go({
      name: 'item',
      query: {
        iid: res
      }
    });
  }
});